use std::{thread, time::Duration};

fn main() {
    println!("=====================================");
    test01_spawn_thread();
    println!("sleep=====================================");
    thread::sleep(Duration::from_millis(100));
    println!("=====================================");
    test02_blocking();
    println!("sleep=====================================");
    thread::sleep(Duration::from_millis(100));
    println!("=====================================");
    test03_move_owner();
    println!("sleep=====================================");
}

fn test01_spawn_thread() {
    thread::spawn(|| {
        for i in 1..10 {
            println!("hi number {i} from the spawned thread");
            thread::sleep(Duration::from_millis(1));
        }
    });

    for i in 1..5 {
        println!("hi number {i} from the main thread!");
        thread::sleep(Duration::from_millis(1));
    }
}

fn test02_blocking() {
    let handle = thread::spawn(|| {
        for i in 1..10 {
            println!("hi number {i} from the spawned thread");
            thread::sleep(Duration::from_millis(1));
        }
    });

    for i in 1..5 {
        println!("hi number {i} from the main thread!");
        thread::sleep(Duration::from_millis(1));
    }

    handle.join().unwrap();
}

fn test03_move_owner() {
    let v = vec![1, 2, 3];

    let handle = thread::spawn(move || {
        println!("Here's a vector: {v:?}");
    });
    //drop(v); // oh no!
    handle.join().unwrap();
}
